﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Configuration;
using System.Net.Mail;
using System.Net;


namespace LKVMLMADMIN.Common
{
    public class MailHelper
    {
       
        public static void SendMail(string toEmailAddress, string subject, string content)
        {
            var fromEmailAddress = ConfigurationManager.AppSettings["FromEmailAddress"].ToString();
            var fromEmailDisplayName = ConfigurationManager.AppSettings["FromEmailDisplayName"].ToString();
            var fromEmailPassword = ConfigurationManager.AppSettings["FromEmailPassword"].ToString();
            var smtpHost = ConfigurationManager.AppSettings["SMTPHost"].ToString();
            var smtpPort = ConfigurationManager.AppSettings["SMTPPort"].ToString();

            bool enabledSsl = bool.Parse(ConfigurationManager.AppSettings["EnabledSSL"].ToString());

            string body = content;
            MailMessage message = new MailMessage(new MailAddress(fromEmailAddress, fromEmailDisplayName), new MailAddress(toEmailAddress));
            message.Subject = subject;
            message.IsBodyHtml = true;
            message.Body = body;

            var client = new SmtpClient();
            client.Credentials = new NetworkCredential(fromEmailAddress, fromEmailPassword);
            client.Host = smtpHost;
            client.EnableSsl = enabledSsl;
            client.Port = !string.IsNullOrEmpty(smtpPort) ? Convert.ToInt32(smtpPort) : 0;
            client.SendAsync(message, null);
        }
        public static void SendMailOutLook(string toEmailAddress, string subject, string content )
        {
            var message = new MailMessage();
            message.To.Add(new MailAddress(toEmailAddress));  // replace with valid value 
            message.From = new MailAddress("no-reply@mybanks.org");  // replace with valid value
            message.Subject = "Your email subject";
            message.Body = content;
            message.IsBodyHtml = true;

            using (var smtp = new SmtpClient())
            {
                var credential = new NetworkCredential
                {
                    UserName = "no-reply@mybanks.org",  // replace with valid value
                    Password = "Mybanks(123456)"  // replace with valid value
                };
                smtp.Credentials = credential;
                smtp.Host = "smtp-mail.outlook.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.SendMailAsync(message);
            }
        }
        public static void SendMailPhGh(string toEmailAddress, string subject, string content)
        {
            var  db = new LKVService.User();
            var mailSend = db.findEmailById(2);
            var fromEmailAddress = mailSend.Email.Trim();
            var fromEmailDisplayName = mailSend.Name;
            var fromEmailPassword = mailSend.Password.Trim();
            var smtpHost = mailSend.SMTPHost;
            var smtpPort = mailSend.SMTPPort;

            bool enabledSsl = true;

            string body = content;
            MailMessage message = new MailMessage(new MailAddress(fromEmailAddress, fromEmailDisplayName), new MailAddress(toEmailAddress));
            message.Subject = subject;
            message.IsBodyHtml = true;
            message.Body = body;

            var client = new SmtpClient();
            client.Credentials = new NetworkCredential(fromEmailAddress, fromEmailPassword);
            client.Host = smtpHost;
            client.EnableSsl = enabledSsl;
            client.Port = !string.IsNullOrEmpty(smtpPort) ? Convert.ToInt32(smtpPort) : 0;
            client.SendAsync(message, null);
        }
    }
}